<?php

/**
 * @copyright: ©2024 永州微聚商 网络科技有限公司
 * @link: https://www.weiJuyunke.com
 */

namespace app\mobile\controller;


use app\admin\model\ActivityConsumer;
use app\admin\model\ConsumerClaimLog;
use app\admin\model\StoreJoinActivity;
use app\BaseController;
use think\App;


/**
 * 用户操作
 *
 * @author Administrator
 *
 */
class Activity extends BaseController
{
    public function activityList(){
        $request = $this->request;
        $param = $request->param();
        $where[] = ['activity.is_delete','=',0];

        $where[] = ['platform_id','=', $request->platform_id];
        $where[]= ['platformInfo.expiration_time','>',time()];
        if (isset( $param['name']) &&  $param['name']){
            $where[] = ['activity.name','like',"%{$param['name']}%"];
        }
        if (isset( $param['status']) &&  $param['status']){
            $where[] = ['activity.status','=',$param['status']];
        }
        $activityId = [];

        if (isset( $param['store_id']) &&  $param['store_id']) {
            if (isset($param['join_status']) && $param['join_status']) {
                $StoreJoinActivityModel = new  StoreJoinActivity();
                if ($param['join_status'] == 1) {
                    $StoreJoinActivityList = $StoreJoinActivityModel->where(['store_id' => $param['store_id']])->select()->toArray();

                    if ($StoreJoinActivityList) {
                        $activityId = array_column($StoreJoinActivityList, 'activity_id');

                        $where[] = ['activity.id', 'not in', $activityId];
                    }
                } elseif ($param['join_status'] == 2) {

                    $StoreJoinActivityList = $StoreJoinActivityModel
                        ->where(['store_id' => $param['store_id']])
                        ->where(['status' => 2])
                        ->select()->toArray();
                    if ($StoreJoinActivityList) {
                        $activityId = array_column($StoreJoinActivityList, 'activity_id');

                        $where[] = ['activity.id', 'in', $activityId];
                    }else{
                        $where[] = ['activity.id', 'in', [0]];
                    }
                }elseif ($param['join_status'] == 3){
                    $StoreJoinActivityList = $StoreJoinActivityModel
                        ->where(['store_id' => $param['store_id']])
                        ->where(['status' => 1])
                        ->select()->toArray();
                    if ($StoreJoinActivityList) {
                        $activityId = array_column($StoreJoinActivityList, 'activity_id');
                        $where[] = ['activity.id', 'in', $activityId];
                    }else{
                        $where[] = ['activity.id', 'in', [0]];
                    }
                }elseif ($param['join_status'] == 4){
                    $StoreJoinActivityList = $StoreJoinActivityModel
                        ->where(['store_id' => $param['store_id']])
                        ->where(['status' => 3])
                        ->select()->toArray();
                    if ($StoreJoinActivityList) {
                        $activityId = array_column($StoreJoinActivityList, 'activity_id');
                        $where[] = ['activity.id', 'in', $activityId];
                    }else{
                        $where[] = ['activity.id', 'in', [0]];
                    }
                }
            }

        }

        $pindex = max(1, intval(input('page')));
        $psize = empty(input('psize')) ? 10 : intval(input('psize'));
        $activityModel = new \app\admin\model\Activity();
        $total = $activityModel
            ->withJoin(['platformInfo'], 'LEFT')->where($where)
            ->where('activity_start_time','<=',time())
            ->where('activity_end_time','>',time())
            ->count();
        if ($total) {
            $list = $activityModel
                ->withJoin(['platformInfo'], 'LEFT')
                ->where($where)
                ->where('activity_start_time','<=',time())
                ->where('activity_end_time','>',time())->order('create_time', 'desc')
                ->limit(($pindex - 1) * $psize, $psize)
                ->select();

            foreach ($list as $key =>$value){
                $list[$key]['activity_start_time']  = date('Y-m-d ',$value['activity_start_time']);
                $list[$key]['activity_end_time']  = date('Y-m-d ',$value['activity_end_time']);
                $activityConsumerModel   = new ActivityConsumer();
                $consumerModel = new \app\admin\model\Consumer();
                $activityConsumerList = $activityConsumerModel->where(['activity_id' => $value['id'],'is_delete' => 0])->select()->toArray();
                $consumerList = [];
                if ($activityConsumerList){
                    $consumerId = array_column($activityConsumerList,'consumer_id');
                    $consumerList = $consumerModel->where('id','in',$consumerId)->limit(0,3)->select()->toArray();
                    $list[$key]['consumer_list'] = $consumerList;
                }else{
                    $list[$key]['consumer_list'] = $consumerList;
                }

            }
        } else {
            $list = array();
        }
        $totalPage = ceil($total / $psize);
        return result(0, 'success', array(
            'list' => $list,
            'total' => $total,
            'pindex' => $pindex,
            'psize' => $psize,
            'totalPage' => $totalPage
        ));
    }

    public function joinActivityStore(){
        $request = $this->request;
        $param = $request->param();
        $storeJoinActivityModel   = new StoreJoinActivity();
        $pindex = max(1, intval(input('page')));
        $psize = empty(input('psize')) ? 10 : intval(input('psize'));
        $storeJoinActivityList =  $storeJoinActivityModel->where(['activity_id' =>$param['id']])->select()->toArray();
        if ($storeJoinActivityList){
            $consumerModel   = new \app\admin\model\Store();
            $storeId = array_column($storeJoinActivityList,'store_id');
            $where = [];
            if (isset($param['store_name']) && $param['store_name']){
                $where[] = ['store_name' ,'like','%'.$param['store_name'].'%'];
            }
            $total = $consumerModel->where($where)->where('id','in',$storeId)->count();
            $storeList = $consumerModel->where($where)->where('id','in',$storeId)->limit(($pindex - 1) * $psize, $psize)->order('create_time','desc') ->select()->toArray();
            $totalPage = ceil($total / $psize);
            return result(0, 'success', array(
                'list' => $storeList,
                'total' => $total,
                'pindex' => $pindex,
                'psize' => $psize,
                'totalPage' => $totalPage
            ));
        }else{
            return result(0, 'success', array(
                'list' => [],
                'total' => 0,
                'pindex' => $pindex,
                'psize' => $psize,
                'totalPage' => 0
            ));
        }
    }


    public function activityInfo(){
        $request = $this->request;
        $param = $request->param();
        $activityModel = new \app\admin\model\Activity();
        $activityInfo = $activityModel->where(['id' => $param['id']])->find();
        $activityInfo['activity_start_time']  = date('Y-m-d ',$activityInfo['activity_start_time']);
        $activityInfo['activity_end_time']  = date('Y-m-d ',$activityInfo['activity_end_time']);
        $activityConsumerModel   = new ActivityConsumer();

        $start_time = strtotime(date('Y-m-d'));
        $end_time = $start_time +86400;

        $consumer_list  = $activityConsumerModel->withJoin([
            'consumerInfo'
        ], 'LEFT')
            ->where('activity_consumer.is_delete','=',0)
           // ->where('start_time','<=',time())
          //  ->where('end_time','>=',time())
           ->where('start_time','>=',$start_time)
             ->where('end_time','<',$end_time)
            ->where(['activity_id' => $activityInfo['id']])->select()->toArray();

        $consumerClaimLogData =[];
        if (isset($param['uid']) &&$param['uid'] ){
            $consumerClaimLog = new ConsumerClaimLog();
            $consumerClaimLogList =  $consumerClaimLog->where(['uid' => $param['uid']])->where('status' ,'<>',0)->select()->toArray();
            $consumerClaimLogData =array_column($consumerClaimLogList,null,'consumer_id');

        }
        foreach ($consumer_list as $key => $value){

            if ($value['start_time'] <= time() &&  $value['end_time'] >=  time() ){
                $consumer_list[$key]['is_can_claim']  = 1;
            }else{
                $consumer_list[$key]['is_can_claim']  = 0;
            }
            $consumer_list[$key]['is_claim'] =0;
            $consumer_list[$key]['start_time']  = date('Y-m-d H:i', $value['start_time']);
            $consumer_list[$key]['end_time']  = date('Y-m-d H:i', $value['end_time']);
            $consumer_list[$key]['consumerInfo']['expire_time']  = date('Y-m-d ', $value['consumerInfo']['expire_time']);
            $consumerList[$key]['is_claim'] = 0;
            if ($consumerClaimLogData){
                if (array_key_exists($value['consumer_id'],$consumerClaimLogData)){
                    $consumer_list[$key]['is_claim'] = 1;
                }
            }
           // $consumer_list[$key]['consumerInfo']['expire_time'] = date('Y-m-d',$value['consumerInfo']['expire_time']);
        }
        $storeJoinActivity =  new StoreJoinActivity();
        $store_join_count = $storeJoinActivity->where(['activity_id' => $activityInfo['id'],'status' => 2])->count();
        $activityInfo['consumer_list']  = $consumer_list;
        $activityInfo['store_join_count']  = $store_join_count;
        $activityInfo['is_join']  = 1;
        if (isset($param['store_id']) &&$param['store_id'] ){
            $storeJoinActivityInfo =   $storeJoinActivity ->where(['store_id' => $param['store_id'],'activity_id' => $activityInfo['id']])->find();
          if ($storeJoinActivityInfo){
              if ($storeJoinActivityInfo['status'] == 2){
                  $activityInfo['is_join']  = 2;
              }else if ($storeJoinActivityInfo['status'] == 1){
                  $activityInfo['is_join']  = 3;
              }else {
                  $activityInfo['is_join']  = 4;
              }
          }
        }
        $storeJoinActivityList =  $storeJoinActivity ->where(['activity_id' => $activityInfo['id'],'status' => 2])->limit(0,8)->select()->toArray();
        if ($storeJoinActivityList){
            $storeId  = array_column($storeJoinActivityList,'store_id');
            $storeModel = new \app\admin\model\Store();
            $activityInfo['store_list']  = $storeModel->where('id','in',$storeId)->select();
        }else{
            $activityInfo['store_list'] = [];
        }
        return result(0, '操作成功',$activityInfo);
    }


    public function activityConsumer(){
        $request = $this->request;
        $param = $request->param();
        $activityConsumerModel   = new ActivityConsumer();
        $pindex = max(1, intval(input('page')));
        $psize = empty(input('psize')) ? 10 : intval(input('psize'));
        $total =  $activityConsumerModel->withJoin('consumerInfo', 'LEFT')
            ->where('activity_consumer.is_delete','=',0)
            ->where('start_time','<=',time())
            ->where('end_time','>=',time())
            ->limit(($pindex - 1) * $psize, $psize)
            ->where(['activity_id' =>$param['id']])->count();
        $activityConsumerList =  $activityConsumerModel
            ->withJoin('consumerInfo', 'LEFT')
            ->where('start_time','<=',time())
            ->where('end_time','>=',time())
             ->limit(($pindex - 1) * $psize, $psize)
            ->where(['activity_id' =>$param['id']])->select()->toArray();

        foreach ($activityConsumerList as $key => $value ){
            $activityConsumerList[$key]['start_time'] = date('Y-m-d H:i',$value['start_time']);
            $activityConsumerList[$key]['end_time'] = date('Y-m-d H:i',$value['end_time']);
        }



        $totalPage = ceil($total / $psize);
        return result(0, 'success', array(
            'list' => $activityConsumerList,
            'total' => $total,
            'pindex' => $pindex,
            'psize' => $psize,
            'totalPage' => $totalPage
        ));
    }
}